<?php
/**
 * Created by PhpStorm.
 * User: HLG
 * Date: 2019/10/14
 * Time: 16:47
 */
namespace app\api\model;

use think\Model;

class Referee extends BaseModel
{
    protected $table = 'yoshop_dealer_referee';

    /**
     * 添加推荐关系
     *
     * @param $user_id
     * @param $refereeId
     * @param $path
     * @return bool
     */
    public function addReferee($user_id,$refereeId=0,$path='')
    {
        //如果自己邀请自己
        if($user_id==$refereeId){
            return false;
        }
        $data = [
            'dealer_id' => $refereeId,
            'user_id' => $user_id,
            'create_time' => time(),
            'path' => $path,
        ];
        $result = $this->allowField(true)->save($data);
        if(!empty($refereeId)){
            //添加上级推荐人数
            $addNum = $this->addRefereeNum($refereeId);
            //添加算力和币
            $addPowerAndCoin = $this->addPowerAndCoin($refereeId);
            //判断是否在活动期间
            $active_id = is_active();
            if($active_id){
                //添加活动邀请记录
                $addInvitationActivity = new InvitationActivity();
                $addRes = $addInvitationActivity->add($refereeId,$user_id,$active_id);
            }else{
                $addRes = true;
            }

        }else{
            return true;
        }
        if($result && $addNum && $addPowerAndCoin && $addRes){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 查询某个用户的推荐信息
     *
     * @param array $where
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserReferee($where = [])
    {
        $data = $this->where($where)->find();
        return $data;
    }

    /**
     * 添加上级推荐人数
     *
     * @param $user_id
     * @return bool
     * @throws \think\Exception
     */
    public function addRefereeNum($user_id)
    {
        $result = $this->where('user_id',$user_id)->setInc('referee_num',1);
        if($result){
            return true;
        }else{
            return false;
        }
    }
    public function addPowerAndCoin($user_id)
    {
        $userBalance = new Balance();
        //获取算力和币设置
        $config = getSetting();
        //查看上级推荐人数
        $data = $this->where('user_id',$user_id)->find();
        if($data['referee_num']<$config['reward_exceed']){ //如果推荐人数小于设定的人数
            $power_array = explode('|',$config['reward_number']);
            $power_key = array_rand($power_array,1);
            $power = $power_array[$power_key];
            $coin_array = explode('|',$config['reward_currency']);
            $coin_key = array_rand($coin_array,1);
            $coin = $coin_array[$coin_key];
            //增加算力
            $userModel = new User();
            $addPower = $userModel->updateNumber(['user_id'=>$user_id],1,'power',$power);
            //增加用户算力记录
            $powerLog = suanli($user_id,$power);
            //增加币
            $addCoin = $userBalance->changeBalance($user_id,$coin,1,8,'邀请好友奖励币'.$coin);
        }else{
            //增加币
            $addCoin = $userBalance->changeBalance($user_id,$config['exceed_number'],1,8,'邀请好友超过50人奖励币'.$config['exceed_number']);
            $addPower = true;
            $powerLog = true;
        }
        if($addPower && $addCoin && $powerLog){
            return true;
        }else{
            return false;
        }
    }
}
